﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using cfg = System.Configuration;
using GoldPrinter;
using WeifenLuo.WinFormsUI.Docking;
namespace BYManageInfoSystem
{
    public partial class Frm_FactoryInfo : DockContent
    {
        private DGBY.BLL.T_ACL_User m_bllT_ACL_User = new DGBY.BLL.T_ACL_User();
        private DGBY.BLL.SJ_SJFeeTab m_bllSJ_SJFeeTab = new DGBY.BLL.SJ_SJFeeTab();
        private DGBY.BLL.ZhuangGuiSiteTab m_bllZhuangGuiSiteTab = new DGBY.BLL.ZhuangGuiSiteTab();
        private DGBY.BLL.BYCompanyTab m_bllBYCompanyTab = new DGBY.BLL.BYCompanyTab();
        private DGBY.BLL.BYDepartTab m_bllBYDepartTab = new DGBY.BLL.BYDepartTab();
        private DGBY.BLL.FactoryGoodsTab m_bllFactoryGoodsTab = new DGBY.BLL.FactoryGoodsTab();
        private DGBY.BLL.FactoryDataTab m_bllFactoryDataTab = new DGBY.BLL.FactoryDataTab();
        private DataSet m_ds = new DataSet();
        private int m_nSelectFactoryID = 0;
        
        protected string templetFilePath = cfg.ConfigurationSettings.AppSettings["TempletFilePath"];
        private bool IsDecimal(string strInput)
        {
            bool bRet = Regex.IsMatch(strInput, @"([1-9]+[0-9]*(\.[0-9]+))|(0(\.[0-9]+))");
            return bRet;
        }

        private bool IsNum(string strInput)
        {
            bool bRet = Regex.IsMatch(strInput, @"^[1-9]+\d*$");
            return bRet;
        }

        private bool ValidDate(string strInput)
        {
            if (strInput.Length != 8)
                return false;
            else
                return IsNum(strInput);
        }

        public bool IsDecimal(ErrorProvider errorProvider1, TextBox TextBox)
        {
            if (TextBox.Text == "")
            {
                errorProvider1.SetError(TextBox, "内容不能为空！");
                return true;
            }
            else if (!IsDecimal(TextBox.Text) && !IsNum(TextBox.Text))
            {
                errorProvider1.SetError(TextBox, "必须输入数字");
                return true;
            }
            else
            {
                errorProvider1.SetError(TextBox, "");
                return false;
            }
        }

        public bool IsNum(ErrorProvider errorProvider1, TextBox TextBox)
        {
            if (TextBox.Text == "")
            {
                errorProvider1.SetError(TextBox, "内容不能为空！");
                return true;
            }
            else if (!IsNum(TextBox.Text))
            {
                errorProvider1.SetError(TextBox, "必须输入数字");
                return true;
            }
            else
            {
                errorProvider1.SetError(TextBox, "");
                return false;
            }
        }

        public bool IsNull(ErrorProvider errorProvider1, TextBox TextBox)
        {
            if (TextBox.Text == "")
            {
                errorProvider1.SetError(TextBox, "内容不能为空！");
                return true;
            }
            else
            {
                errorProvider1.SetError(TextBox, "");
                return false;
            }
        }

        private void InitCombox()
        {
            DataSet ds = m_bllBYDepartTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxDep.DataSource = ds.Tables[0];
                this.comboBoxDep.DisplayMember = "部门名称";
                this.comboBoxDep.ValueMember = "BYDepartTabID";
                this.comboBoxDep.SelectedIndex = 0;
            }
            ds = m_bllBYCompanyTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxComp.DataSource = ds.Tables[0];
                this.comboBoxComp.DisplayMember = "经营单位名称";
                this.comboBoxComp.ValueMember = "BYCompanyTabID";
                this.comboBoxComp.SelectedIndex = 0;
            }

            ds = m_bllFactoryGoodsTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxGoodsCate.DataSource = ds.Tables[0];
                this.comboBoxGoodsCate.DisplayMember = "货品名称";
                this.comboBoxGoodsCate.ValueMember = "FactoryGoodsTabID";
                this.comboBoxGoodsCate.SelectedIndex = 0;
            }
            ds = m_bllZhuangGuiSiteTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxZhenQu.DataSource = ds.Tables[0];
                this.comboBoxZhenQu.DisplayMember = "装柜镇区";
                this.comboBoxZhenQu.ValueMember = "ZhuangGuiSiteTabID";
                this.comboBoxZhenQu.SelectedIndex = 0;
            }

            ds = m_bllT_ACL_User.GetList("Name != 'System1'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxFinPerson.DataSource = ds.Tables[0];
                this.comboBoxFinPerson.DisplayMember = "用户名称";
                this.comboBoxFinPerson.ValueMember = "UserPasswordTabID";
                this.comboBoxFinPerson.SelectedIndex = 0;
            }
        }

        public Frm_FactoryInfo()
        {
            InitializeComponent();
            InitCombox();
        }

        private void buttonFacAddr_Click(object sender, EventArgs e)
        {
           
            //if (this.textBoxFactoryName.Text == "")
            //{
            //    MessageBox.Show("请先选择工厂!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}
            int nId = 0;
            DataSet ds = m_bllFactoryDataTab.GetList("FactoryName = '" + this.textBoxFactoryName.Text + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                nId = Convert.ToInt32(ds.Tables[0].Rows[0]["FactoryDataTabID"].ToString(), 10);
            }
            Frm_FactoryAddr frm_FactoryAddr = new Frm_FactoryAddr(nId);
            frm_FactoryAddr.StartPosition = FormStartPosition.CenterScreen;
            frm_FactoryAddr.ShowDialog();
        }

        private void buttonFacContactPerson_Click(object sender, EventArgs e)
        {
           
            //if (this.textBoxFactoryName.Text == "")
            //{
            //    MessageBox.Show("请先选择工厂!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}
            int nId = 0;
            DataSet ds = m_bllFactoryDataTab.GetList("FactoryName = '" + this.textBoxFactoryName.Text + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                nId = Convert.ToInt32(ds.Tables[0].Rows[0]["FactoryDataTabID"].ToString(), 10);
            }
            Frm_FactoryConPerson frm_FactoryConPerson = new Frm_FactoryConPerson(nId);
            frm_FactoryConPerson.StartPosition = FormStartPosition.CenterScreen;
            frm_FactoryConPerson.ShowDialog();
            
        }

        private void buttonFacBankAndAccount_Click(object sender, EventArgs e)
        {
            //if (Frm_Main.m_strLoginUser != "钟运甜")
            //{
            //    MessageBox.Show("对不起，你没有权限修改工厂!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}
            if (this.textBoxFactoryName.Text == "")
            {
                MessageBox.Show("请先选择工厂!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            int nId = 0;
            DataSet ds = m_bllFactoryDataTab.GetList("FactoryName = '" + this.textBoxFactoryName.Text + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                nId = Convert.ToInt32(ds.Tables[0].Rows[0]["FactoryDataTabID"].ToString(), 10);
            }
            Frm_FactoryBankAndAccount frm_FactoryBankAndAccount = new Frm_FactoryBankAndAccount(nId);
            frm_FactoryBankAndAccount.StartPosition = FormStartPosition.CenterScreen;
            frm_FactoryBankAndAccount.ShowDialog();
        }

        private void buttonFacConsume_Click(object sender, EventArgs e)
        {
            Frm_FactoryConsume frm_FactoryConsume = new Frm_FactoryConsume();
            frm_FactoryConsume.StartPosition = FormStartPosition.CenterScreen;
            frm_FactoryConsume.ShowDialog();
        }

        private void ShowAllRecordInDataGridView(string strWhere)
        {
            m_ds = m_bllFactoryDataTab.GetList(strWhere);

            ultraGrid1.DataSource = null;
            
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                ultraGrid1.DataSource = m_ds.Tables[0];
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns[1].Hidden = true;
                ultraGrid1.DisplayLayout.Bands[0].Columns[2].Hidden = true;



                if (Frm_Main.m_bCompleteVer == false)
                {

                    ultraGrid1.DisplayLayout.Bands[0].Columns["是否费用特殊工厂"].Hidden = true;
                    ultraGrid1.DisplayLayout.Bands[0].Columns["退税率"].Hidden = true;
                    ultraGrid1.DisplayLayout.Bands[0].Columns["出口与结汇期初余额"].Hidden = true;
                    ultraGrid1.DisplayLayout.Bands[0].Columns["开票与退税期初余额"].Hidden = true;
                    ultraGrid1.DisplayLayout.Bands[0].Columns["账面期初余额"].Hidden = true;
                    ultraGrid1.DisplayLayout.Bands[0].Columns["补本金期初余额"].Hidden = true;
                    ultraGrid1.DisplayLayout.Bands[0].Columns["补退税期初余额"].Hidden = true;
                    ultraGrid1.DisplayLayout.Bands[0].Columns["优惠汇率"].Hidden = true;

                    ultraGrid1.DisplayLayout.Bands[0].Columns["公式选择"].Hidden = true;
                    ultraGrid1.DisplayLayout.Bands[0].Columns["系数A"].Hidden = true;
    

                }
            }
            this.textBoxFactoryCode.Text = m_bllFactoryDataTab.GetMaxFacCode().ToString();
        }

        private void buttonShowAllRecords_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView("");
            
        }



        private void dataGridViewFacInfoList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            //string strBYCompID = this.dataGridViewFacInfoList.Rows[this.dataGridViewFacInfoList.CurrentCell.RowIndex].Cells[0].Value.ToString();
            //m_nSelectFactoryID = Convert.ToInt32(strBYCompID, 10);
            //cellclick();
        }

        private void cellclick()
        {
            DGBY.Model.FactoryDataTab modelFactoryDataTab = m_bllFactoryDataTab.GetModel(m_nSelectFactoryID);
            if (modelFactoryDataTab != null)
            {
                this.textBoxFactoryCode.Text = modelFactoryDataTab.FactoryCode.ToString();
                this.textBoxFactoryName.Text = modelFactoryDataTab.FactoryName;
                this.textBoxFacAbbrName.Text = modelFactoryDataTab.FactoryNameJX;
                this.textBoxFactoryPhone.Text = modelFactoryDataTab.FactoryTel;
                this.textBoxFactoryFax.Text = modelFactoryDataTab.FactoryFax;
                this.textBoxManagerName.Text = modelFactoryDataTab.ManagerName;
                this.textBoxManagerPhone.Text = modelFactoryDataTab.ManagerTel;
                this.textBoxInvoiceFaceValue.Text = modelFactoryDataTab.InvoiceCost.ToString();
                this.checkBoxGangBiChuHuo.Checked = modelFactoryDataTab.bitFiledReserv1;
                //this.checkBoxFeeSpecialFac.Checked = modelFactoryDataTab.isFeeSpecialFac;
                this.textBoxBackTaxRate.Text = modelFactoryDataTab.TuiShuiLv.ToString();
                this.textBoxPayTaxCode.Text = modelFactoryDataTab.NaShuiCode;
                this.textBoxRegFactoryName.Text = modelFactoryDataTab.GSDJZFactoryName;
                this.textBoxTaxPayerIndentityCode.Text = modelFactoryDataTab.FacCountPeoCode;
                this.richTextBoxNote.Text = modelFactoryDataTab.Note;
                //this.textBoxGoodsName.Text = modelFactoryDataTab.FactoryGoodsName;
                this.textBoxQiChuRemainJinE.Text = modelFactoryDataTab.QiChuRemainJinE.ToString();

                this.textBoxQiChuCKJH.Text = modelFactoryDataTab.CKJHQiChuRemainJinEUSD.ToString();
                this.textBoxQiChuKPTS.Text = modelFactoryDataTab.KPTSQiChuRemainJinEUSD.ToString();
                this.textBoxQiChuZM.Text = modelFactoryDataTab.ZMQiChuRemainJinERMB.ToString();
                this.textBoxQiChuBBJ.Text = modelFactoryDataTab.BBJQiChuRemainJinERMB.ToString();
                this.textBoxQiChuBTS.Text = modelFactoryDataTab.BTSQiChuRemainJinERMB.ToString();



                if (modelFactoryDataTab.BYDepartTabID != null)
                    this.comboBoxDep.SelectedValue = modelFactoryDataTab.BYDepartTabID;
                if (modelFactoryDataTab.BYCompanyTabID != null)
                    this.comboBoxComp.SelectedValue = modelFactoryDataTab.BYCompanyTabID;
                if (modelFactoryDataTab.FactoryGoodsTabID != null)
                    this.comboBoxGoodsCate.SelectedValue = modelFactoryDataTab.FactoryGoodsTabID;
                if (modelFactoryDataTab.ZhuangGuiSiteTabID != null)
                    this.comboBoxZhenQu.SelectedValue = modelFactoryDataTab.ZhuangGuiSiteTabID;
                      
                if (modelFactoryDataTab.UserPasswordTabID != null)
                    this.comboBoxFinPerson.SelectedValue = modelFactoryDataTab.UserPasswordTabID;

                
                this.textBoxFormulaSelector.Text = modelFactoryDataTab.PriceTaxFormulaSelector.ToString();
                this.textBoxFactorA.Text = modelFactoryDataTab.FactorA.ToString();
     

            }
            else
            {
                this.textBoxFactoryCode.Text = "";
                this.textBoxFactoryName.Text = "";
                this.textBoxFacAbbrName.Text = "";
                this.textBoxFactoryPhone.Text = "";
                this.textBoxFactoryFax.Text = "";
                this.textBoxManagerName.Text = "";
                this.textBoxManagerPhone.Text = "";
                this.textBoxInvoiceFaceValue.Text = "";
                this.textBoxBackTaxRate.Text = "";
                this.textBoxPayTaxCode.Text = "";
                this.textBoxRegFactoryName.Text = "";
                this.textBoxTaxPayerIndentityCode.Text = "";
                this.richTextBoxNote.Text = "";
                this.textBoxFormulaSelector.Text = "";
                this.textBoxFactorA.Text = "";
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            if (!Frm_Main.m_lstPermitPerson.Contains(Frm_Main.m_strLoginUser))
            {
                MessageBox.Show("对不起，你没有权限修改工厂!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            try
            {
                if (ClassInputValid.IsDecimalButCanEmpty(this.errorProvider1, this.textBoxInvoiceFaceValue))
                    return;
                if (ClassInputValid.IsDecimalButCanEmpty(this.errorProvider1, this.textBoxBackTaxRate))
                    return;
                if (ClassInputValid.IsDecimalButCanEmpty(this.errorProvider1, this.textBoxQiChuRemainJinE))
                    return;
                if (ClassInputValid.IsDecimalButCanEmpty(this.errorProvider1, this.textBoxQiChuCKJH))
                    return;
                if (ClassInputValid.IsDecimalButCanEmpty(this.errorProvider1, this.textBoxQiChuKPTS))
                    return;
                if (ClassInputValid.IsDecimalButCanEmpty(this.errorProvider1, this.textBoxQiChuZM))
                    return;
                if (ClassInputValid.IsDecimalButCanEmpty(this.errorProvider1, this.textBoxQiChuBBJ))
                    return;
                if (ClassInputValid.IsDecimalButCanEmpty(this.errorProvider1, this.textBoxQiChuBTS))
                    return;



                DGBY.Model.FactoryDataTab modelFactoryDataTab = new DGBY.Model.FactoryDataTab();
                modelFactoryDataTab.FactoryCode = m_bllFactoryDataTab.GetMaxFacCode();
                modelFactoryDataTab.FactoryName = this.textBoxFactoryName.Text;
                modelFactoryDataTab.FactoryNameJX = this.textBoxFacAbbrName.Text;
                modelFactoryDataTab.FactoryTel = this.textBoxFactoryPhone.Text;
                modelFactoryDataTab.FactoryFax = this.textBoxFactoryFax.Text;
                modelFactoryDataTab.ManagerName = this.textBoxManagerName.Text;
                modelFactoryDataTab.ManagerTel = this.textBoxManagerPhone.Text;
                //modelFactoryDataTab.isFeeSpecialFac = this.checkBoxFeeSpecialFac.Checked;
                modelFactoryDataTab.isFeeSpecialFac = false;
                modelFactoryDataTab.bitFiledReserv1 = this.checkBoxGangBiChuHuo.Checked;
                
                modelFactoryDataTab.InvoiceCost = this.textBoxInvoiceFaceValue.Text == "" ? 0 : Convert.ToDecimal(this.textBoxInvoiceFaceValue.Text);
                modelFactoryDataTab.TuiShuiLv = this.textBoxBackTaxRate.Text == "" ? 0 : Convert.ToDecimal(this.textBoxBackTaxRate.Text);
                modelFactoryDataTab.NaShuiCode = this.textBoxPayTaxCode.Text;
                modelFactoryDataTab.GSDJZFactoryName = this.textBoxRegFactoryName.Text;
                modelFactoryDataTab.FacCountPeoCode = this.textBoxTaxPayerIndentityCode.Text;
                modelFactoryDataTab.Note = this.richTextBoxNote.Text;
                modelFactoryDataTab.FactoryGoodsName = "";
                modelFactoryDataTab.PriceTaxFormulaSelector = this.textBoxFormulaSelector.Text == "" ? 0 : Convert.ToInt32(this.textBoxFormulaSelector.Text, 10);
                modelFactoryDataTab.FactorA = this.textBoxFactorA.Text == "" ? 0 : Convert.ToDecimal(this.textBoxFactorA.Text);

                modelFactoryDataTab.QiChuRemainJinE = this.textBoxQiChuRemainJinE.Text == "" ? 0 : Convert.ToDecimal(this.textBoxQiChuRemainJinE.Text);
                modelFactoryDataTab.CKJHQiChuRemainJinEUSD = this.textBoxQiChuCKJH.Text == "" ? 0 : Convert.ToDecimal(this.textBoxQiChuCKJH.Text);
                modelFactoryDataTab.KPTSQiChuRemainJinEUSD = this.textBoxQiChuKPTS.Text == "" ? 0 : Convert.ToDecimal(this.textBoxQiChuKPTS.Text);
                modelFactoryDataTab.ZMQiChuRemainJinERMB = this.textBoxQiChuZM.Text == "" ? 0 : Convert.ToDecimal(this.textBoxQiChuZM.Text);
                modelFactoryDataTab.BBJQiChuRemainJinERMB = this.textBoxQiChuBBJ.Text == "" ? 0 : Convert.ToDecimal(this.textBoxQiChuBBJ.Text);
                modelFactoryDataTab.BTSQiChuRemainJinERMB = this.textBoxQiChuBTS.Text == "" ? 0 : Convert.ToDecimal(this.textBoxQiChuBTS.Text);
                if (this.comboBoxDep.SelectedValue != null)
                    modelFactoryDataTab.BYDepartTabID = Convert.ToInt32(this.comboBoxDep.SelectedValue.ToString(), 10);
                if (this.comboBoxComp.SelectedValue != null)
                    modelFactoryDataTab.BYCompanyTabID = Convert.ToInt32(this.comboBoxComp.SelectedValue.ToString(), 10);
                if (this.comboBoxGoodsCate.SelectedValue != null)
                    modelFactoryDataTab.FactoryGoodsTabID = Convert.ToInt32(this.comboBoxGoodsCate.SelectedValue.ToString(), 10);
                if (this.comboBoxZhenQu.SelectedValue != null)
                    modelFactoryDataTab.ZhuangGuiSiteTabID = Convert.ToInt32(this.comboBoxZhenQu.SelectedValue.ToString(), 10);
                if (this.comboBoxFinPerson.SelectedValue != null)
                    modelFactoryDataTab.UserPasswordTabID = Convert.ToInt32(this.comboBoxFinPerson.SelectedValue.ToString(), 10);

                modelFactoryDataTab.bitFiledReserv1 = false;
                modelFactoryDataTab.bitFiledReserv2 = false;

                modelFactoryDataTab.stringFiledReserv1 = "";
                modelFactoryDataTab.stringFiledReserv2 = "";
                modelFactoryDataTab.stringFiledReserv3 = "";
                modelFactoryDataTab.stringFiledReserv4 = "";

                modelFactoryDataTab.intFiledReserv1 = 0; //制单人员
                modelFactoryDataTab.intFiledReserv2 = 0;
                modelFactoryDataTab.intFiledReserv3 = 0;
                modelFactoryDataTab.intFiledReserv4 = 0;

                DateTime dtNow = System.DateTime.Now.Date;
                modelFactoryDataTab.datetimeFiledReserv1 = dtNow;
                modelFactoryDataTab.datetimeFiledReserv2 = dtNow;

                modelFactoryDataTab.decimalFiledReserv1 = 0;
                modelFactoryDataTab.decimalFiledReserv2 = 0;
                modelFactoryDataTab.decimalFiledReserv3 = 0;
                modelFactoryDataTab.decimalFiledReserv4 = 0;

                int nResult = m_bllFactoryDataTab.Add(modelFactoryDataTab);

                if (nResult != 1)
                {
                    m_nSelectFactoryID = m_bllFactoryDataTab.GetMaxId() - 1;
                    ShowAllRecordInDataGridView("FactoryName = '" + this.textBoxFactoryName.Text + "'");
                    //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                DGBY.Model.SJ_SJFeeTab modelSJ_SJFeeTab = new DGBY.Model.SJ_SJFeeTab();
                modelSJ_SJFeeTab.ValidDate = dtNow;
                modelSJ_SJFeeTab.ValidDateEnd = DateTime.Parse("2199-01-01"); 
                if (modelFactoryDataTab.FactoryGoodsTabID != 1)//家具
                {
                    modelSJ_SJFeeTab.SJFeeFactor = 1;
                    modelSJ_SJFeeTab.SJFeePriceEachGui = 0;
                    modelSJ_SJFeeTab.SJFeeAddValue = 20;
                }
                else
                {
                    modelSJ_SJFeeTab.SJFeeFactor = 0;
                    modelSJ_SJFeeTab.SJFeePriceEachGui = 200;
                    modelSJ_SJFeeTab.SJFeeAddValue = 0;
                }
                modelSJ_SJFeeTab.FactoryDataTabID = nResult;

                m_bllSJ_SJFeeTab.Add(modelSJ_SJFeeTab);
                Frm_Main.Log2DB(this.Text, "增加", modelFactoryDataTab.FactoryNameJX);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
          
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            if (!Frm_Main.m_lstPermitPerson.Contains(Frm_Main.m_strLoginUser))
            {
                MessageBox.Show("对不起，你没有权限修改工厂!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            try
            {
                DGBY.Model.FactoryDataTab modelFactoryDataTabOld = m_bllFactoryDataTab.GetModel(m_nSelectFactoryID);
                if (modelFactoryDataTabOld == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (ClassInputValid.IsDecimalButCanEmpty(this.errorProvider1, this.textBoxInvoiceFaceValue))
                    return;
                if (ClassInputValid.IsDecimalButCanEmpty(this.errorProvider1, this.textBoxBackTaxRate))
                    return;
                if (ClassInputValid.IsDecimalButCanEmpty(this.errorProvider1, this.textBoxQiChuRemainJinE))
                    return;
                if (ClassInputValid.IsDecimalButCanEmpty(this.errorProvider1, this.textBoxQiChuCKJH))
                    return;
                if (ClassInputValid.IsDecimalButCanEmpty(this.errorProvider1, this.textBoxQiChuKPTS))
                    return;
                if (ClassInputValid.IsDecimalButCanEmpty(this.errorProvider1, this.textBoxQiChuZM))
                    return;
                if (ClassInputValid.IsDecimalButCanEmpty(this.errorProvider1, this.textBoxQiChuBBJ))
                    return;
                if (ClassInputValid.IsDecimalButCanEmpty(this.errorProvider1, this.textBoxQiChuBTS))
                    return;

                //DGBY.Model.FactoryDataTab modelFactoryDataTab = new DGBY.Model.FactoryDataTab();

                //modelFactoryDataTabOld.FactoryDataTabID = m_nSelectFactoryID;
                modelFactoryDataTabOld.FactoryCode = modelFactoryDataTabOld.FactoryCode;
               // modelFactoryDataTab.FactoryCode = Convert.ToInt32(this.textBoxFactoryCode.Text, 10);
                modelFactoryDataTabOld.FactoryName = this.textBoxFactoryName.Text;
                modelFactoryDataTabOld.FactoryNameJX = this.textBoxFacAbbrName.Text;
                modelFactoryDataTabOld.FactoryTel = this.textBoxFactoryPhone.Text;
                modelFactoryDataTabOld.FactoryFax = this.textBoxFactoryFax.Text;
                modelFactoryDataTabOld.ManagerName = this.textBoxManagerName.Text;
                modelFactoryDataTabOld.ManagerTel = this.textBoxManagerPhone.Text;
                //modelFactoryDataTabOld.isFeeSpecialFac = this.checkBoxFeeSpecialFac.Checked;
                modelFactoryDataTabOld.isFeeSpecialFac = false;
                modelFactoryDataTabOld.bitFiledReserv1 = this.checkBoxGangBiChuHuo.Checked;
                modelFactoryDataTabOld.InvoiceCost = Convert.ToDecimal(this.textBoxInvoiceFaceValue.Text);
                modelFactoryDataTabOld.TuiShuiLv = Convert.ToDecimal(this.textBoxBackTaxRate.Text);
                modelFactoryDataTabOld.NaShuiCode = this.textBoxPayTaxCode.Text;
                modelFactoryDataTabOld.GSDJZFactoryName = this.textBoxRegFactoryName.Text;
                modelFactoryDataTabOld.FacCountPeoCode = this.textBoxTaxPayerIndentityCode.Text;
                modelFactoryDataTabOld.Note = this.richTextBoxNote.Text;
                modelFactoryDataTabOld.PriceTaxFormulaSelector = Convert.ToInt32(this.textBoxFormulaSelector.Text, 10);
                modelFactoryDataTabOld.FactorA = Convert.ToDecimal(this.textBoxFactorA.Text);
                modelFactoryDataTabOld.FactoryGoodsName = "";
                modelFactoryDataTabOld.QiChuRemainJinE = Convert.ToDecimal(this.textBoxQiChuRemainJinE.Text);
                modelFactoryDataTabOld.CKJHQiChuRemainJinEUSD = this.textBoxQiChuCKJH.Text == "" ? 0 : Convert.ToDecimal(this.textBoxQiChuCKJH.Text);
                modelFactoryDataTabOld.KPTSQiChuRemainJinEUSD = this.textBoxQiChuKPTS.Text == "" ? 0 : Convert.ToDecimal(this.textBoxQiChuKPTS.Text);
                modelFactoryDataTabOld.ZMQiChuRemainJinERMB = this.textBoxQiChuZM.Text == "" ? 0 : Convert.ToDecimal(this.textBoxQiChuZM.Text);
                modelFactoryDataTabOld.BBJQiChuRemainJinERMB = this.textBoxQiChuBBJ.Text == "" ? 0 : Convert.ToDecimal(this.textBoxQiChuBBJ.Text);
                modelFactoryDataTabOld.BTSQiChuRemainJinERMB = this.textBoxQiChuBTS.Text == "" ? 0 : Convert.ToDecimal(this.textBoxQiChuBTS.Text);
               
                if (this.comboBoxDep.SelectedValue != null)
                    modelFactoryDataTabOld.BYDepartTabID = Convert.ToInt32(this.comboBoxDep.SelectedValue.ToString(), 10);
                if (this.comboBoxComp.SelectedValue != null)
                    modelFactoryDataTabOld.BYCompanyTabID = Convert.ToInt32(this.comboBoxComp.SelectedValue.ToString(), 10);
                if (this.comboBoxGoodsCate.SelectedValue != null)
                    modelFactoryDataTabOld.FactoryGoodsTabID = Convert.ToInt32(this.comboBoxGoodsCate.SelectedValue.ToString(), 10);
                if (this.comboBoxZhenQu.SelectedValue != null)
                    modelFactoryDataTabOld.ZhuangGuiSiteTabID = Convert.ToInt32(this.comboBoxZhenQu.SelectedValue.ToString(), 10);
                if (this.comboBoxFinPerson.SelectedValue != null)
                    modelFactoryDataTabOld.UserPasswordTabID = Convert.ToInt32(this.comboBoxFinPerson.SelectedValue.ToString(), 10);

                m_bllFactoryDataTab.Update(modelFactoryDataTabOld);
                Frm_Main.Log2DB(this.Text, "修改", modelFactoryDataTabOld.FactoryNameJX);
                ShowAllRecordInDataGridView("FactoryName = '" + this.textBoxFactoryName.Text + "'");
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
          
        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            if (!Frm_Main.m_lstPermitPerson.Contains(Frm_Main.m_strLoginUser))
            {
                MessageBox.Show("对不起，你没有权限修改工厂!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (m_bllFactoryDataTab.GetModel(m_nSelectFactoryID) == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要删除该记录么，删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            m_bllFactoryDataTab.Delete(m_nSelectFactoryID);
            Frm_Main.Log2DB(this.Text, "删除", this.textBoxFactoryName.Text);
            ShowAllRecordInDataGridView("FactoryName = '" + this.textBoxFactoryName.Text + "'");

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            cellclick();
        }

        private void Frm_FactoryInfo_Load(object sender, EventArgs e)
        {
            this.TabPageContextMenu = new System.Windows.Forms.ContextMenu();
            this.TabPageContextMenu.Tag = this.Text;
            this.TabPageContextMenu.Popup += new System.EventHandler(Frm_Main.contextMenu1_Popup);
            


            this.label6.Visible = Frm_Main.m_bCompleteVer;
            this.textBoxFactorA.Visible = Frm_Main.m_bCompleteVer;
            this.label5.Visible = Frm_Main.m_bCompleteVer;
            this.textBoxFormulaSelector.Visible = Frm_Main.m_bCompleteVer;
            this.labelBackTaxRate.Visible = Frm_Main.m_bCompleteVer;
            this.textBoxBackTaxRate.Visible = Frm_Main.m_bCompleteVer;
            this.textBox1.Visible = Frm_Main.m_bCompleteVer;
            this.label7.Visible = Frm_Main.m_bCompleteVer;
            this.textBoxQiChuCKJH.Visible = Frm_Main.m_bCompleteVer;
            this.label8.Visible = Frm_Main.m_bCompleteVer;
            this.textBoxQiChuKPTS.Visible = Frm_Main.m_bCompleteVer;

            this.label9.Visible = Frm_Main.m_bCompleteVer;
            this.textBoxQiChuZM.Visible = Frm_Main.m_bCompleteVer;
            this.label10.Visible = Frm_Main.m_bCompleteVer;
            this.textBoxQiChuBBJ.Visible = Frm_Main.m_bCompleteVer;
            this.label11.Visible = Frm_Main.m_bCompleteVer;
            this.textBoxQiChuBTS.Visible = Frm_Main.m_bCompleteVer;
            this.label2.Visible = Frm_Main.m_bCompleteVer;
            this.textBoxQiChuRemainJinE.Visible = Frm_Main.m_bCompleteVer;

            if (Frm_Main.m_strLoginUser == "管理员")
            {
                this.buttonDel.Visible = true;
            }
        }

        

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            ClassUltraGridCommonSet.InitUltraWinGridRowNumSetting(e);
            e.Layout.Bands[0].Columns[3].Header.Appearance.ForeColor = Color.Blue;
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            string strBYCompID = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells[0].Text;
            m_nSelectFactoryID = Convert.ToInt32(strBYCompID, 10);
            cellclick();
        }



    }
}